package com.xiyue.lenovo.fragmenttest;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.xiyue.lenovo.fragmenttest.model.BookContent;

public class BookListFragment extends ListFragment {
    private Callbacks mCallback;
    //该Fragment将通过该接口与它所在的Activity交互
    public interface Callbacks{
        public void onItemSelected(Integer id);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //为该ListFragment设置Adapter
        setListAdapter(new ArrayAdapter<BookContent.Book>(getActivity(),
                android.R.layout.simple_list_item_activated_1
                ,android.R.id.text1,BookContent.ITEMS));
    }
    //当该Fragment被添加、显示到Activity时，回调该方法

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //如果Activity没有实现Callbacs接口，抛出异常
        if(!(activity instanceof Callbacks)){
            throw new IllegalStateException(
                    "BookListFragment所在的Activity必须实现CallBack接口！"
            );
        }
        //把该Activity当成Callbacks对象
        mCallback = (Callbacks) activity;
    }
    //当该Fragment从它所属的Activity中北删除时回调该方法

    @Override
    public void onDetach() {
        super.onDetach();
        //将mCallbacks赋值为null
        mCallback=null;
    }
    //当用户单击某列表项时激发该回调方法

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //激发mCallbacks的onItemSelected方法
        mCallback.onItemSelected(BookContent.ITEMS.get(position).id);
    }
    public void setActivateOnItemClick(boolean activateOnItemClick){
        getListView().setChoiceMode(activateOnItemClick?
                ListView.CHOICE_MODE_SINGLE:ListView.CHOICE_MODE_NONE);
    }
}
